<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

     
      <el-form-item label="设备名称" prop="phoneName">
        <el-input clearable v-model="queryParams.phoneName" placeholder="请输入设备名称" ></el-input>
      </el-form-item>
      <el-form-item label="在线状态" prop="phoneState">
        <el-select clearable v-model="queryParams.phoneState" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="在线" value="在线"></el-option>
          <el-option label="离线" value="离线"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNum">
        <el-input clearable v-model="queryParams.phoneNum" placeholder="请输入手机号" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:notice:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="!selectedRows.length"
          @click="handleBatchDelete" v-hasPermi="['system:notice:remove']">删除</el-button>
      </el-col>
      <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
        v-hasPermi="['system:notice:export']">导出</el-button>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <!-- 表格 -->
    <el-table ref="multipleTable" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="phoneName" label="设备名称" width="200" show-overflow-tooltip sortable></el-table-column>
      <el-table-column prop="phoneRecordno" label="设备序列号" width="300" show-overflow-tooltip sortable></el-table-column>
      <el-table-column prop="phoneNum" label="手机号码" width="300" show-overflow-tooltip sortable></el-table-column>

      <el-table-column prop="isOnline" label="在线状态" show-overflow-tooltip sortable>
        <template slot-scope="scope">
          {{ scope.row.phoneState }}
        </template>
      </el-table-column> <el-table-column prop="phoneFirstAccess" label="首次接入时间" width="200"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentPageChange"
      :current-page="queryParams.pageNum" :page-size="queryParams.pageSize" :page-sizes="[1, 10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" class="pagination">
    </el-pagination>

    <el-dialog title="终端设备详情" :visible.sync="detailDialogVisible" width="50%">
      <div class="apk-detail-content">
        <el-card class="box-card" v-if="phoneDetail">
          <div slot="header" class="clearfix">
            <span>基础信息</span>
          </div>
          <el-row :gutter="20" class="form">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                设备序列号：{{ phoneDetail.phoneRecordno }}
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                设备品牌型号：{{ phoneDetail.phoneBand }}
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                操作系统：{{ phoneDetail.phoneOs }}
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                用户：{{ phoneDetail.createBy }}
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20" class="form">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                手机号码：{{ phoneDetail.phoneNum }}
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                状态：{{ phoneDetail.phoneState }}
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                安装apk数量：29
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                风险apk数量：12
              </div>
            </el-col>
          </el-row>
        </el-card>
        <!-- 下面部分 -->
        <div>
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <div style="margin-bottom: 10px;">
                APP列表
              </div>

              <el-table :data="tableData2" border style="width: 100%">
                <el-table-column prop="appName" label="APP名称" width="180" />
                <el-table-column prop="appVersion" label="版本" width="120" />
                <el-table-column prop="appUpdatedate" label="发布时间" width="180" />
                <el-table-column prop="appDeveloper" label="所属公司" width="180" />
                <el-table-column prop="appSize" label="大小" />
                <el-table-column prop="riskStatus" label="风险情况" width="120" />
              </el-table>
            </div>
          </el-card>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :title="title + '终端设备'" :visible.sync="addDialogVisible" width="50%">
      <div class="apk-detail-content">
        <el-card class="box-card">
          <el-form ref="formPhone" :model="formPhone" :rules="rules" label-width="110px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="设备名称" prop="phoneName">
                  <el-input v-model="formPhone.phoneName"></el-input>
                </el-form-item>
                <el-form-item label="品牌型号" prop="phoneBand">
                  <el-input v-model="formPhone.phoneBand"></el-input>
                </el-form-item>
                <el-form-item label="序列号" prop="phoneRecordno">
                  <el-input v-model="formPhone.phoneRecordno"></el-input>
                </el-form-item>
                <el-form-item label="操作系统" prop="phoneOs">
                  <el-input v-model="formPhone.phoneOs"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="手机号" prop="phoneNum">
                  <el-input v-model="formPhone.phoneNum"></el-input>
                </el-form-item>
                <el-form-item label="在线状态" prop="phoneState">
                  <el-select v-model="formPhone.phoneState" placeholder="请选择">
                    <el-option label="在线" value="在线"></el-option>
                    <el-option label="离线" value="离线"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="首次接入时间" prop="phoneFirstAccess">
                  <el-date-picker v-model="formPhone.phoneFirstAccess" align="right" type="date" placeholder="选择日期"
                    value-format="yyyy.MM.dd">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" size="small">提交</el-button>
              <el-button size="small" @click="cancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listPhone, detailPhone, listApk, addPhone, delPhone, updatePhone } from '@/api/applicationService/terminal.js'
import { getInfo } from "@/api/login";

export default {
  data() {
    return {
      searchKeyword: '',
      currentPage: 1,
      pageSize: 6,
      totalDataCount: 0,
      tableData: [],
      filteredTableData: [],
      selectedRows: [],
      editForm: {},
      detailDialogVisible: false,
      addDialogVisible: false,
      detailRow: {},
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneName: "",
        phoneState: "",
        phoneNum: "",
      },
      multiple: null,
      tableData2: [
        {
          name: "抖音",
          version: "1.2.3",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "访问和收集个人信息",
        },
        {
          name: "爱奇艺",
          version: "2.2.3",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "收集通讯录信息",
        },
        {
          name: "腾讯视频",
          version: "2.12.3",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "查看相册",
        },
        {
          name: "酷狗音乐",
          version: "2.2.4",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "植入病毒",
        },
        {
          name: "QQ音乐",
          version: "3.12.1",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "恶意吸费",
        },
      ],
      phoneDetail: null,
      formPhone: {
        phoneName: "",
        phoneBand: "",
        phoneRecordno: "",
        phoneOs: "",
        phoneNum: "",
        phoneState: "在线",
        phoneFirstAccess: "",
        createBy: "",
      },
      user: null,
      rules: {
        phoneName: [
          { required: true, message: '请输入设备名称', trigger: 'blur' },
        ],
      },
      total: 0,
      title: "",
    };
  },
  computed: {
    currentTableData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.filteredTableData.slice(start, end);
    },
  },
  methods: {
    /** 搜索按钮操作 */
    handleSearch() {
      //格式校验  如果有手机号就校验手机号
      if (this.queryParams.phoneNum) {
        if (!/^\d{11}$/.test(this.queryParams.phoneNum)) {
          this.$message.error('手机号格式不正确');
          return;
        }
      }
      this.getList();
      this.currentPage = 1;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm("formPhone");
      this.addDialogVisible = true;
      this.title = "新增";
    },
    filterData() {
      this.filteredTableData = this.tableData.filter(item => {
        return Object.keys(item).some(key =>
          String(item[key]).includes(this.searchKeyword)
        );
      });
      this.totalDataCount = this.filteredTableData.length;
    },
    handleSizeChange(newSize) {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = newSize;
      this.getList();
    },
    handleCurrentPageChange(newPage) {
      this.queryParams.pageNum = newPage
      this.getList()
    },
    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },
    // 详情
    handleDetail(row) {
      this.$router.push("/applicationService/terminal/detail?id=" + row.phoneId);
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.title = '编辑'
      this.addDialogVisible = true;
      this.formPhone = JSON.parse(JSON.stringify(row))
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delPhone(row.phoneId).then(res => {
          if (res.code == 200) {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
        });
      });
    },
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message({
          message: '请至少选择一个设备进行删除',
          type: 'warning'
        });
        return;
      }
      this.$confirm('确认删除这些任务吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = []
        this.selectedRows.map(ele => {
          ids.push(ele.phoneId)
        })
        delPhone(ids).then(res => {
          if (res.code == 200) {
            this.getList()
            this.$message({
              type: 'success',
              message: '批量删除成功!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          message: '已取消批量删除',
          type: 'info'
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('combat/phone/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    },
    // 获取列表
    getList() {
      listPhone(this.queryParams).then(res => {
        this.tableData = res.rows || []
        this.total = res.total
      })
    },

    handleClick(tab, event) {
      this.queryParams.phoneState = tab.name
      this.getList()
    },
    clearEditData() {
      this.editForm = {
        deviceName: '',
        serialNumber: '',
        phoneNumber: '',
        onlineStatus: '',
        firstAccessTime: '',
      };
    },
    // 提交
    onSubmit() {
      this.$refs['formPhone'].validate((valid) => {
        if (valid) {
          if (this.title == '新增') {
            this.formPhone.createBy = this.user.userName
            addPhone(this.formPhone).then(res => {
              if (res.code == 200) {
                this.getList()
                this.addDialogVisible = false
                this.$message({
                  type: 'success',
                  message: '新增成功!',
                });
              }
            })
          } else {
            updatePhone(this.formPhone).then(res => {
              if (res.code == 200) {
                this.getList()
                this.addDialogVisible = false
                this.$message({
                  type: 'success',
                  message: '编辑成功!',
                });
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    },
    // 取消
    cancel() {
      this.resetForm("formPhone");
      this.addDialogVisible = false
    },
  },
  created() {
    this.tableData = [
      {
        deviceName: '设备1',
        serialNumber: 'ocr-license-2542',
        phoneNumber: '14566845000',
        isOnline: true,
        firstAccessTime: '2022-01-01 10:00:00',
      },
      {
        deviceName: '设备2',
        serialNumber: 'ocr-license-54245',
        phoneNumber: '14157515000',
        isOnline: true, firstAccessTime: '2022-01-02 10:00:00',
      },
      {
        deviceName: '设备3',
        serialNumber: 'ocr-license-3456',
        phoneNumber: '13800138000',
        isOnline: true, firstAccessTime: '2022-01-03 10:00:00',
      },
      {
        deviceName: '设备4',
        serialNumber: 'ocr-license-4567',
        phoneNumber: '13456789000',
        isOnline: true, firstAccessTime: '2022-01-04 10:00:00',
      },
      {
        deviceName: '设备5',
        serialNumber: 'ocr-license-5678',
        phoneNumber: '13456789001',
        isOnline: false, firstAccessTime: '2022-01-05 10:00:00',
      },
      {
        deviceName: '设备6',
        serialNumber: 'ocr-license-6789',
        phoneNumber: '13456789002',
        onlineStatus: '在线',
        firstAccessTime: '2022-01-06 10:00:00',
      },
      {
        deviceName: '设备7',
        serialNumber: 'ocr-license-7890',
        phoneNumber: '13456789003',
        isOnline: false, firstAccessTime: '2022-01-07 10:00:00',
      },
      {
        deviceName: '设备8',
        serialNumber: 'ocr-license-8901',
        phoneNumber: '13456789004',
        isOnline: true, firstAccessTime: '2022-01-08 10:00:00',
      },
      {
        deviceName: '设备9',
        serialNumber: 'ocr-license-9012',
        phoneNumber: '13456789005',
        isOnline: false, firstAccessTime: '2022-01-09 10:00:00',
      },
      {
        deviceName: '设备10',
        serialNumber: 'ocr-license-0123',
        phoneNumber: '13456750006',
        isOnline: true, firstAccessTime: '2022-01-10 10:00:00',
      },
      {
        deviceName: '设备11',
        serialNumber: 'ocr-license-1234',
        phoneNumber: '13456750007',
        isOnline: false, firstAccessTime: '2022-01-11 10:00:00',
      },
      {
        deviceName: '设备12',
        serialNumber: 'ocr-license-2345',
        phoneNumber: '13456750008',
        isOnline: true, firstAccessTime: '2022-01-12 10:00:00',
      },
    ];
    this.filterData();
    getInfo().then(res => {
      this.user = res.user
    })
  },
  mounted() {
    this.tableData = []
    this.tableData2 = []
    this.getList()
  }
};
</script>
<style scoped>
::v-deep .el-pagination {
  float: inline-end;
}
</style>
